#include "StdAfx.h"
#include "FinalizerRunLogger.h"
#include <sstream>

FinalizerRunLogger::FinalizerRunLogger(const ObjectTracker &objectTracker)
: m_ObjectTracker(objectTracker)

{
	// outputWriter.open("E:\\Test.log");
}

FinalizerRunLogger::~FinalizerRunLogger(void)
{
}

STDMETHODIMP FinalizerRunLogger::Initialize(const std::wstring &logFilePath)
{
	outputWriter.open(logFilePath.c_str());
	return S_OK;
}

STDMETHODIMP FinalizerRunLogger::FinalizeableObjectQueued(DWORD finalizerFlags, ObjectID objectID)
{
	ObjectCreationSnapshot snapshot;

	if (FAILED(m_ObjectTracker.GetCreationSnapshot(objectID, &snapshot)))
		return E_FAIL;

	std::wostringstream fullStackAsText;
	std::vector<std::wstring> stack = snapshot.stack;

	for (std::vector<std::wstring>::iterator iter = stack.begin(); iter != stack.end(); ++iter)
	{
		fullStackAsText << *iter << std::endl;
	}

	outputWriter
		<< objectID << std::endl
		<< snapshot.clrClass.fullTypeName << std::endl 
		<< fullStackAsText.str().c_str() << std::endl 
		<< std::endl;
	outputWriter.flush();

	return S_OK;
}

STDMETHODIMP FinalizerRunLogger::Shutdown()
{
	outputWriter.close();
	return S_OK;
}